package cn.newgrand.netcall.component;

/**
 * 项(item)基类，
 * 与ItemGroup合用展示效果
 * 如联系人界面item样式的展现
 */


import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.PresenceManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettings;
import org.jivesoftware.sparkimpl.plugin.layout.LayoutSettingsManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Represent a single contact within the <code>ContactList</code>.
 */
public class BaseItem extends JPanel {
    private JLabel imageLabel;
    private JLabel displayNameLabel;
    private JLabel descriptionLabel;
    private String url;
    private String id;
    private String pid;
    private Icon icon;
    private String name;

    private String status;
    private String groupName;

    boolean available;

    private Presence presence;

    private String hash = "";

    private File contactsDir;

    private JLabel sideIcon;
    
    private JLabel sideIconBorder;

    private int fontSize;

    private int iconSize;

    private boolean avatarsShowing;

//    public BaseItem(String id,String name,String url){
//    	
//    }
    
    /**
     * Creates a new instance of a item.
     * 
     * @param id  item unique identification 
     * @param name  the name to display 
     * @param url   to url some where 
     * @param pid parent id
     */
    public BaseItem(String id, String name,Icon icon, String url,String pid,String displayName) {
        setLayout(new GridBagLayout());

        // Set Default Font
        final LocalPreferences pref = SettingsManager.getLocalPreferences();
        fontSize = pref.getContactListFontSize();
        iconSize = pref.getContactListIconSize();
        avatarsShowing = pref.areAvatarsVisible();

        // Set default presence
        presence = new Presence(Presence.Type.unavailable);

//        contactsDir = new File(SparkManager.getUserDirectory(), "contacts");

        displayNameLabel = new JLabel();
        descriptionLabel = new JLabel();
        imageLabel = new JLabel();
        if(null!=icon){
        	imageLabel.setIcon(icon);
        }
//        sideIcon = new JLabel();
        sideIconBorder =new JLabel();
        displayNameLabel.setHorizontalTextPosition(JLabel.LEFT);
        displayNameLabel.setHorizontalAlignment(JLabel.LEFT);
        if(null==displayName){
        	displayNameLabel.setText(name);
        }else
        	displayNameLabel.setText(displayName);

        descriptionLabel.setFont(new Font("Dialog", Font.PLAIN, fontSize));
        descriptionLabel.setForeground((Color)UIManager.get("ContactItemDescription.foreground"));
        descriptionLabel.setHorizontalTextPosition(JLabel.LEFT);
        descriptionLabel.setHorizontalAlignment(JLabel.LEFT);

        this.setOpaque(true);

        add(imageLabel, new GridBagConstraints(0, 0, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 15, 3, 0), 0, 0));
        add(displayNameLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
        add(descriptionLabel, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 2, 0), 0, 0));

        this.id = id;
        this.name = name;
        this.url = url;
        this.pid = pid;
        setDisplayName();
    }

	/**
	 * Returns the name that should be displayed to represent the the contact.
	 * If an alias has been set, this alias will be returned. If no alias has
	 * been set, the nickname will be returned. If that hasn't been set either,
	 * the JID will be returned.
	 * 
	 * @return a name suitable to be displayed
	 */
    public String getDisplayName() {
    	return name;
	}

    /**
	 * Returns the alias of the contact. Note that for typical user-interface
	 * related tasks, you probably should use {@link #getDisplayName()} instead.
	 * 
	 * @return the contact alias.
	 */
	public String getName() {
		return name;
	}
    
    /**
     * Sets the alias of the contact. 
     *
     * @param alias the contact alias.
     */
    public void setName(String name) {
    	this.name = name;
    	setDisplayName();
    }

    /**
	 * Updates the displayed name for the contact. This method tries to use an
	 * alias first. If that's not set, the nickname will be used instead. If
	 * that's not set either, the JID of the user will be used.
	 */
    private void setDisplayName() {
    	final String displayName = getDisplayName();
    	
        int nickLength = displayName.length();
        
        LayoutSettings settings = LayoutSettingsManager.getLayoutSettings();
        int windowWidth = (int)Math.round((settings.getMainWindowHeight() / 15.2));
        
        if (nickLength > windowWidth) {
            displayNameLabel.setText(StringUtils.unescapeNode(displayName).substring(0, windowWidth) + "...");
        } else {
            displayNameLabel.setText(StringUtils.unescapeNode(displayName));
        }    	
    }
    
    /**
     * 返回item的内容id
     *
     */
    public String getID() {
        return id;
    }

    /**
     * Returns the icon showing the contacts current state or presence.
     *
     * @return the icon.
     */
    public Icon getIcon() {
        return icon;
    }

    /**
     * Sets the current icon to use.
     *
     * @param icon the current icon to use.
     */
    public void setIcon(Icon icon) {
        this.icon = icon;
        imageLabel.setIcon(icon);
    }

    /**
     * Returns the name of the <code>ContactGroup</code> that this contact belongs to.
     *
     * @return the name of the <code>ContactGroup</code>.
     */
    public String getGroupName() {
        return groupName;
    }

    /**
     * Sets the name of the <code>ContactGrouop</code> that this contact belongs to.
     *
     * @param groupName the name of the ContactGroup.
     */
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
    /**
     * Returns the <code>JLabel</code> showing the users nickname.
     *
     * @return the nickname label.
     */
    public JLabel getNicknameLabel() {
        return displayNameLabel;
    }

    /**
     * Returns the <code>JLabel</code> representing the description.
     *
     * @return the description label.
     */
    public JLabel getDescriptionLabel() {
        return descriptionLabel;
    }
    /**
     * Checks to see if the hash already exists.
     *
     * @param hash the hash.
     * @return true if the hash exists, otherwise false.
     */
    private boolean hashExists(String hash) {
        contactsDir.mkdirs();

        final File imageFile = new File(contactsDir, hash);
        return imageFile.exists();
    }

    /**
     * Returns the url of the avatar belonging to this contact.
     *
     * @return the url of the avatar.
     * @throws MalformedURLException thrown if the address is invalid.
     */
    public String getURL(){
        return url;
    }

    /**
     * Persists the avatar locally based on the new hash.
     */
    public void setURL() {
        this.url = url;
    }

    public String toString() {
        return displayNameLabel.getText();
    }

    /**
     * The icon to use to show extra information about this contact. An example would be to
     * represent that this user is from a 3rd party transport.
     *
     * @param icon the icon to use.
     */
    public void setSideIcon(Icon icon) {
        sideIcon.setIcon(icon);
    }
    
    public void setSideIconBorder(Icon icon){
    	sideIconBorder.setIcon(icon);
    }
}
